using System.Collections.Generic;
using UnityEngine;

public class LevelController : MonoBehaviour
{
	private string levelsIniFilePath = "DataFiles/levels";

	private string drinksIniFilePath = "DataFiles/drinks";

	private string glassPrefabPath = "Prefabs/Glasses/";

	private float levelTime;

	private float levelExcellentTime;

	private List<GameObject> glasses;

	private int currentDrink;

	private Transform glassSpawnPoint;

	[HideInInspector]
	public bool canWatchAdToRestartDrink = true;

	[HideInInspector]
	public float scorePerLiquid;

	private int liquidsInLevel;

	[HideInInspector]
	public float currentScore;

	private float savedScoreAtChangingDrink;

	public float savedTimeAtChangingDrink;

	private void Awake()
	{
		glassSpawnPoint = GetComponent<GameController>().glassSpawnPoint;
		glasses = new List<GameObject>();
	}

	public List<string> LoadLevel(int levelId)
	{
		currentDrink = 0;
		currentScore = 0f;
		canWatchAdToRestartDrink = true;
		INIParser iNIParser = new INIParser();
		TextAsset name = Resources.Load<TextAsset>(levelsIniFilePath);
		iNIParser.Open(name);
		string sectionName = "level_" + levelId;
		List<string> list = new List<string>();
		if (iNIParser.IsSectionExists(sectionName))
		{
			for (int i = 1; iNIParser.IsKeyExists(sectionName, "order_" + i); i++)
			{
				string item = iNIParser.ReadValue(sectionName, "order_" + i, string.Empty);
				list.Add(item);
			}
			levelTime = (float)iNIParser.ReadValue(sectionName, "time", 0.0);
			levelExcellentTime = (float)iNIParser.ReadValue(sectionName, "excellent_time", 0.0);
			savedTimeAtChangingDrink = levelTime;
		}
		CheckIfCreateOrRemoveMoveFingerReminder(levelId);
		iNIParser.Close();
		return list;
	}

	public void SpawnGlassesForDrinks(List<string> drinkNames, bool outsideScreen, bool spawnWithScale = true)
	{
		float num = 1f;
		if (spawnWithScale)
		{
			num = 0.333f;
		}
		float d = 0f;
		if (outsideScreen)
		{
			d = 12f * num;
		}
		INIParser iNIParser = new INIParser();
		TextAsset name = Resources.Load<TextAsset>(drinksIniFilePath);
		iNIParser.Open(name);
		for (int i = 0; i < drinkNames.Count; i++)
		{
			string str = "Glass_" + iNIParser.ReadValue(drinkNames[i], "glass_id", string.Empty);
			GameObject original = Resources.Load(glassPrefabPath + str) as GameObject;
			Vector3 vector = Camera.main.ScreenToWorldPoint(Vector3.right * Screen.width);
			GameObject gameObject = UnityEngine.Object.Instantiate(original, glassSpawnPoint.position + Vector3.right * d + Vector3.right * vector.x * 1.15f * i * num, default(Quaternion));
			gameObject.transform.localScale = new Vector3(num, num, 1f);
			GlassAddons component = gameObject.GetComponent<GlassAddons>();
			if (component != null)
			{
				component.SetGlassBorderAddon(iNIParser.ReadValue(drinkNames[i], "addition_id", 0));
				component.SetStrawAddon(iNIParser.ReadValue(drinkNames[i], "straw_id", 0));
			}
			gameObject.name = drinkNames[i];
			glasses.Add(gameObject);
			DrinkTargets component2 = gameObject.GetComponent<DrinkTargets>();
			for (int j = 1; iNIParser.IsKeyExists(drinkNames[i], "liquid_" + j); j++)
			{
				LiquidType liquidType = LiquidTypes.GetLiquidType(iNIParser.ReadValue(drinkNames[i], "liquid_" + j, string.Empty));
				float item = (float)iNIParser.ReadValue(drinkNames[i], "liquid_" + j + "_percentage", 0.0);
				component2.targetLiquidTypes.Add(liquidType);
				component2.targetLiquidHeights.Add(item);
			}
		}
		iNIParser.Close();
		CalculateScoreForLevel();
	}

	private void CalculateScoreForLevel()
	{
		liquidsInLevel = 0;
		foreach (GameObject glass in glasses)
		{
			liquidsInLevel += glass.GetComponent<DrinkTargets>().targetLiquidTypes.Count;
		}
		scorePerLiquid = 3.75f / (float)liquidsInLevel;
	}

	public List<LiquidType> GetLiquidTypesOfCurrentDrink()
	{
		return glasses[currentDrink].GetComponent<DrinkTargets>().targetLiquidTypes;
	}

	public GameObject GetCurrentGlassObject()
	{
		return glasses[currentDrink];
	}

	public Transform[] GetArrayOfGlassTransforms()
	{
		Transform[] array = new Transform[glasses.Count];
		for (int i = 0; i < glasses.Count; i++)
		{
			array[i] = glasses[i].transform;
		}
		return array;
	}

	public void SetNextCurrentDrink()
	{
		currentDrink++;
		savedScoreAtChangingDrink = currentScore;
		savedTimeAtChangingDrink = UnityEngine.Object.FindObjectOfType<TimeCounter>().GetCurrentTime();
	}

	public void MoveDrinks(float targetPositionX)
	{
		Vector3 position = glasses[0].transform.position;
		position.x = targetPositionX;
		glasses[0].GetComponent<GlassMoveTo>().Play(position);
		if (glasses.Count > 1)
		{
			Vector3 position2 = glasses[1].transform.position;
			float x = position2.x;
			Vector3 position3 = glasses[0].transform.position;
			float d = x - position3.x;
			for (int i = 1; i < glasses.Count; i++)
			{
				glasses[i].GetComponent<GlassMoveTo>().Play(position + Vector3.right * d * i);
			}
		}
	}

	public void MoveDrinks()
	{
		for (int num = glasses.Count; num > 1; num--)
		{
			glasses[num - 1].GetComponent<GlassMoveTo>().Play(glasses[num - 2].transform.position);
		}
		glasses[0].GetComponent<GlassMoveTo>().Play(glasses[0].transform.position + Vector3.left * 20f);
	}

	public void MoveDrinksOutsideScreen()
	{
		float targetPositionX = (float)glasses.Count * -8.5f;
		MoveDrinks(targetPositionX);
	}

	public void ShowCurrentDrinkTargets()
	{
		glasses[currentDrink].GetComponent<DrinkTargets>().SetTargets();
		glasses[currentDrink].GetComponent<DrinkTargets>().ShowTargetLines();
	}

	public int GetDrinksInLevelAmount()
	{
		return glasses.Count;
	}

	public float GetTimeForLevel()
	{
		return levelTime;
	}

	public float GetExcellentTimeForLevel()
	{
		return levelExcellentTime;
	}

	public void DestroyGlasses()
	{
		foreach (GameObject glass in glasses)
		{
			UnityEngine.Object.Destroy(glass);
		}
		glasses.Clear();
	}

	public void ResetCurrentDrink()
	{
		GameObject gameObject = glasses[currentDrink];
		gameObject.GetComponent<GlassFillingUpController>().ClearGlassFromLiquids();
		gameObject.GetComponent<DrinkTargets>().SetInitialCurrentTargetLine();
		currentScore = savedScoreAtChangingDrink;
		canWatchAdToRestartDrink = false;
	}

	public LiquidType CurrentLiquidType()
	{
		try
		{
			return glasses[currentDrink].GetComponent<DrinkTargets>().GetCurrentTargetLine().liquidType;
		}
		catch
		{
			return LiquidType.Null;
		}
	}

	private void CheckIfCreateOrRemoveMoveFingerReminder(float levelId)
	{
		//MoveFingerReminder component = GetComponent<MoveFingerReminder>();
		//if (component == null)
		//{
		//	if (levelId == 1f)
		//	{
		//		base.gameObject.AddComponent<MoveFingerReminder>();
		//	}
		//}
		//else if (levelId != 1f)
		//{
		//	component.DestroyAnimation();
		//}
		//else
		//{
		//	component.ResetAnimation();
		//}
	}
}
